use  "${Input}\mhd2_dta\mhdhrt2.dta" 
generate NTransRec=1 if hrt11==1 
generate NParTransRec=((hrt14>=10 & hrt14<=13) | (hrt14>=20 & hrt14<=21)) 
generate NSibTransRec=(hrt14>=14 & hrt14<=19)
replace hrt20=. if hrt20>=999997 
replace hrt21a=. if hrt21a>=999997 
generate AmtTransRec=hrt20+hrt21a
replace AmtTransRec=0 if hrt11==3
generate AmtTransParRec=hrt20+hrt21a if ((hrt14>=10 & hrt14<=13) | (hrt14>=20 & hrt14<=21)) 
replace AmtTransParRec=0 if hrt11==3
generate AmtTransSibRec=hrt20+hrt21a if (hrt14>=14 & hrt14<=19)
replace AmtTransSibRec=0 if hrt11==3
rename hrt12 person
sort case person
collapse (sum) NTransRec AmtTransRec NParTransRec NSibTransRec AmtTransParRec AmtTransSibRec , by(case person)
sort case person
save ${Output}\TransfersReceived, replace

use ${Output}\MainFile
sort case person
merge case person using ${Output}\TransfersReceived, nokeep
rename _merge _mergeTransRec
save, replace

